package com.lc.demo.service.impl;

import com.lc.demo.common.bo.AddUserBO;
import com.lc.demo.common.vo.UserVO;
import com.lc.demo.model.Users;
import com.lc.demo.repository.UsersRepository;
import com.lc.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UsersRepository usersRepository;


    @Override
    public Boolean addUser(AddUserBO addUserBO) {
        Users user = new Users();
        user.setName(addUserBO.getName());
        user.setAge(addUserBO.getAge());
        user.setSex(addUserBO.getSex());
        // 游离
        try {
            this.usersRepository.save(user);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public List<UserVO> getUsers() {
        List<UserVO> res = new ArrayList<>();
        List<Users> list = usersRepository.findAll();
        if (list.isEmpty()) {
            return res;
        }
        for (Users user : list) {
            UserVO vo = new UserVO();
            vo.setAge(user.getAge());
            vo.setName(user.getName());
            vo.setSex(user.getSex());
            res.add(vo);
        }
        return res;
    }
}
